<template>
    <div class="user_contextmenu_bt">
        <template v-if="$store.state.user_data">
            <div ref="user_content" class="content" @click="user_contextmenu_show=!user_contextmenu_show">
                {{$store.state.user_data.name}}
                <i class="el-icon-caret-bottom"></i>
            </div>
        </template>
        <template v-else>
            <div class="login" @click="on_login">
                登陆
            </div>
        </template>
        <div ref="user_contextmenu" class="user_contextmenu" v-if="user_contextmenu_show">
            <div class="top">
                <div class="left">
                    <img src="https://thirdqq.qlogo.cn/g?b=sdk&k=icY3pm3vCXkWU0vVPRxTv3w&s=640&t=1593436844" alt="">
                </div>
                <div class="right">
                    <div class="top">
                        {{$store.state.user_data.name}}
                    </div>
                    <div class="bottom">
                        未开通会员
                    </div>
                </div>
            </div>
            <div class="bottom">
                <div class="item" @click="on_delete_all">
                    删除账号下所有文件
                </div>
                <div class="my_divider">
                    <el-divider></el-divider>
                </div>
                <div class="item" @click="on_quit">
                    退出登陆
                </div>
            </div>
        </div>
    </div>
</template>

<script>
export default { //用户按钮组件，点击显示目录
    name: 'user_contextmenu_bt',
    data(){
        return{
            user_contextmenu_show:false, //控制用户目录菜单的显示
            user_from:{
                password:"",
            },
        };
    },
    created(){
        document.addEventListener('click', event => {  //如果元素不是在组件上点击的则隐藏该组件
            const e = event || window.event;
            if (this.$refs.user_contextmenu && !this.$refs.user_contextmenu.contains(e.target) && !this.$refs.user_content.contains(e.target)) {
                this.user_contextmenu_show = false;
            }
        });
    },
    methods:{
        on_login(){ //登陆按钮点击事件（跳转到登陆页面）
            this.$router.push('/login');
        },
        on_quit(){ //退出登陆
            this.$store.commit('remove_user_data');
            this.$router.push('/login');
        },
        on_delete_all(){
            this.$confirm('此操作将永久删除账号下所有文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$store.commit('set_loading',{
                    state:true,
                });
                //获取已选择的文件提交
                this.$axios.json_post("file/deleteAll").then(()=>{
                    this.$message({
                        type: 'success',
                        message: '删除成功!',
                    });
                }).finally(()=>{
                    this.$store.commit('set_loading',{
                        state:false,
                    });
                    this.$store.state.update_data = !this.$store.state.update_data;
                });
            }).catch(() => {
                return;     
            });
        }
    },
}
</script>

<style scoped lang='scss'>
.user_contextmenu_bt{
    position: relative;
    z-index: 999;
    .content{
        font-size: 15px;
        line-height: 100%;
        cursor: pointer;
        padding: 8px;
        border-radius: 2px;
        transition: all 0.2s;
        color: #646464;
        background-color: #eaeaea72;
        box-shadow: 0 1px 3px 0 rgba(0,0,0,.1), 0 1px 2px 0 rgba(0,0,0,.06);
        i{
            margin-left: 5px;
        }
        &:hover{
            background-color: #eaeaea;
        }
    }
    .login{
        font-size: 15px;
        line-height: 100%;
        cursor: pointer;
        padding: 8px 15px;
        border-radius: 2px;
        transition: all 0.2s;
        color: #ffffff;
        font-weight: bold;
        letter-spacing: 2px;
        background-color: #eaeaea72;
        background-image: linear-gradient(to right, #0acffe 0%, #495aff 100%);
        &:hover{
            background-image: linear-gradient(#0acffe 0%, #0acffe 100%);
        }
    }
    .user_contextmenu{
        position:absolute;
        top: 31px;
        right: 0;
        z-index: 9999;
        width:250px;
        height: fit-content;
        background-color: white;
        border: 1px solid #c8ccd3;
        border-radius: 2px;
        box-shadow: 0 1px 4px 0 rgba(15,32,65,.2);
        >.top{
            display: flex;
            justify-content: flex-start;
            align-items: center;
            padding: 25px;
            box-sizing: border-box;
            background-color: whitesmoke;
            >.left{
                margin-right: 10px;
                img{
                    height: 45px;
                    width: 45px;
                    border-radius: 3px;
                }
            }
            >.right{
                display: flex;
                flex-direction: column;
                justify-content: center;
                height:45px;
                text-align: left;
                >.top{
                    color: #020202;
                    font-size: 18px;
                    line-height: 100%;
                    margin-bottom: 10px;
                }
                >.bottom{
                    font-size: 12px;
                    color: #777;
                    line-height: 100%;
                }
            }
        }
        >.bottom{
            padding: 5px 0;
            >.my_divider{
                padding: 5px 0;
                box-sizing: border-box;
                .el-divider--horizontal{
                    margin:0;
                }
            }
            >.item{
                height: 35px;
                display: flex;
                flex-direction: row;
                justify-content: flex-start;
                align-items: center;
                font-size: 14px;
                color: #020202;
                line-height: 100%;
                padding: 0 25px;
                transition: all 0.2s;
                cursor: pointer;
                &:hover{
                    background-color: #f4f4f4;
                }
            }
        }
    }
}
</style>
